package com.topboya.linker.ui.presenter;

import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.model.response.SelectFileResponse;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.SelectFileView;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;


import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class SelectFilePresenter extends BasePresenter<SelectFileView> {
    private static final String TAG = "SelectFilePresenter:";
    public SelectFilePresenter(BaseActivity context) {
        super(context);
    }

    public void getProductFileList(String productId,String fileType){

            mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
            ApiRetrofit.getInstance().getProductFileList(productId,fileType)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(selectfileresponse -> {
                        String success = selectfileresponse.getMeta().getSuccess();
                        mContext.hideWaitingDialog();

                        if (null != success && Boolean.parseBoolean(success)) {

                            List<SelectFileResponse.FileList> fileLists = selectfileresponse.getData().getList();
                             getView().getProductFileList( fileLists);
                        }

                    }, this::loginError);
        }


    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }
}
